
FreeText = { };

function LuaC.FreeText( text, font, x, y, time, speed, r, g, b )
	
	
	table.insert( FreeText, {
		
		Text = text,
		Font = font,
		Speed = speed,
		x = x,
		y = y,
		NextChar = GetTicks() + speed,
		EndTime = GetTicks() + time,
		CurLen = 0,
		MaxLen = string.len( text ),
		r = r or 255,
		g = g or 255,
		b = b or 255,
		
	} );	
	
end

function DrawFreeText()
	
	for k, v in pairs( FreeText ) do
	
		Draw.SetColor255( v.r, v.g, v.b, 255 );
		Draw.Text( v.Font, string.sub( v.Text, 1, v.CurLen ), v.x, v.y ); 
		
		if( GetTicks() > v.NextChar and v.CurLen < v.MaxLen ) then
			
			v.CurLen = v.CurLen + 1;
			v.NextChar = v.Speed + GetTicks();
			
		end

		if( v.EndTime < GetTicks() ) then
			
			FreeText[k] = nil;
			
		end
		
	end
	
end

BattleText = { };

function LuaC.BattleText( text, time, speed )
	
	table.insert( BattleText, {
		
		Text = text,
		Font = "Uni16Bold",
		Speed = speed,
		NextChar = GetTicks() + speed,
		EndTime = GetTicks() + time,
		CurLen = 0,
		MaxLen = string.len( text ),
		
	} );
	
end

function DrawBattleText()
	
	local yoffset = 0;
	
	for k, v in pairs( BattleText ) do
		
		if( yoffset > 0 ) then
			
			Draw.SetColor255( 0, 0, 0, 255 );
			Draw.Rect( 0, yoffset + 118, 800, 7 );
			
		end
		
		Draw.SetColor255( 30, 30, 30, 255 );
		Draw.Rect( 0, 120 + yoffset, 800, 20 );
		
		Draw.SetColor255( 255, 255, 255, 255 );
		Draw.Text( v.Font, string.sub( v.Text, 1, v.CurLen ), 5, 119 + yoffset ); 
		
		if( GetTicks() > v.NextChar and v.CurLen < v.MaxLen ) then
			
			v.CurLen = v.CurLen + 1;
			v.NextChar = v.Speed + GetTicks();
			
		end
		
		yoffset = yoffset + 22;
		
		if( v.EndTime < GetTicks() ) then
			
			BattleText[k] = nil;
			
		end
		
	end
	
end

PopupText = { };

function LuaC.PopupText( text, x, y, speed, fadeout )

	table.insert( PopupText, {

		Text = text, 
		x = x,
		y = y,
		Speed = speed,
		Font = "Uni26Bold",
		Fadeout = fadeout,
		Alpha = 255,
		StartTime = GetTicks(),
		startY = y,
		OrigSpeed = speed,

	} );

end

MsgBox = { };

function LuaC.ClearMsgBoxes()
	
	MsgBox = nil;
	MsgBox = { };
	
end

function LuaC.MessageBox2( text, ent, ox, oy, delay, speed )
	
	local pos = ent:GetPos();
	
	LuaC.MessageBox( text, pos.x + ox, pos.y + ox, function()
	
		local npos = ent:GetPos();
		
		return npos.x + ox, npos.y + oy;
	
	end, delay, speed );
	
end

function LuaC.MessageBox( text, mx, my, func, delay, speed, mouth )
	
	delay = delay or 0;
	speed = speed or 80;
	
	if( mouth == nil ) then
		mouth = true;
	end
	
	local v_str = string.explode( text, " " );
	
	local fmt_str = "";
	local est_size = 0;
	
	local v_fmtstr = { };
	
	local width = 0;
	local remainder = false;
	
	for k, v in pairs( v_str ) do
		
		est_size = est_size + string.len( v ) * 10;	
		
		if( est_size > 100 ) then
			
			est_size = 0;
			
			fmt_str = fmt_str .. v;
			
			local t_width = GetStringWidth( "Uni14", fmt_str );
			
			table.insert( v_fmtstr, fmt_str );
			
			if( width < t_width ) then
				
				width = t_width;
				
			end
			
			fmt_str = "";
			remainder = false;
			
		else
			
			fmt_str = fmt_str .. v .. " ";
			remainder = true;
			
		end
		
	end
	
	if( remainder ) then
		
		local t_width = GetStringWidth( "Uni14", fmt_str );
		
		if( width < t_width ) then
				
			width = t_width;
				
		end
		
		table.insert( v_fmtstr, fmt_str );
		
	end
	
	local height = #v_fmtstr * 14;
	
	local x = mx - width * .5;
	local y = my - height - 35;
	
	if( not mouth ) then
		
		x = mx;
		y = my;
		
	end
	
	table.insert( MsgBox, { 

		v_text = v_fmtstr,
		text = text,
		x = x, y = y,
		width = width + 10,
		height = height + 10,
		mouthx = mx,
		mouthy = my,
		mouth = mouth,
		think = func,
		endtime = delay + GetTicks(),
		next_char_time = 0, cur_len = 0,
		len = string.len( text ),
		speed = speed,

	} );
		
	
end

function DrawMsgBoxes()
	
	local campos = GetCameraPos();
	
	if( G.RPGBattle ) then
		
		campos = { x = 0, y = 0 };
		
	end
	
	for k, v in pairs( MsgBox ) do
		
		if( v.think ) then
			
			local nx, ny = v.think();
			
			v.x = nx - v.width * .5;
			v.y = ny - v.height - 35;
			
		end
		
		Draw.SetColor( 1, 1, 1, 1 );
		
		Draw.Texture( "Graphics/UI/msgbox_topleft.png", v.x - campos.x, v.y - campos.y, 4, 3 );
		Draw.Texture( "Graphics/UI/msgbox_top.png", v.x - campos.x + 4, v.y - campos.y, v.width - 7, 3 );
		Draw.Texture( "Graphics/UI/msgbox_left.png", v.x - campos.x, v.y - campos.y + 3, 4, v.height - 6 );
		Draw.Texture( "Graphics/UI/msgbox_bottomleft.png", v.x - campos.x, v.y - campos.y + v.height - 3, 4, 3 );
		Draw.Texture( "Graphics/UI/msgbox_bottom.png", v.x - campos.x + 4, v.y - campos.y + v.height - 3, v.width - 7, 3 );
		Draw.Texture( "Graphics/UI/msgbox_topright.png", v.x - campos.x + v.width - 3, v.y - campos.y, 3, 3 );
		Draw.Texture( "Graphics/UI/msgbox_right.png", v.x - campos.x + v.width - 3, v.y - campos.y + 3, 3, v.height - 6 );
		Draw.Texture( "Graphics/UI/msgbox_bottomright.png", v.x - campos.x + v.width - 3, v.y - campos.y + v.height - 3, 3, 3 );
		
		if( v.mouth ) then
		
			if( v.mouthx > v.x - campos.x + v.width * .5 ) then
			
				Draw.Texture( "Graphics/UI/msgbox_leftmouth.png", v.x - campos.x + v.width * .55, v.y - campos.y + v.height - 2, 11, 9 );
		
			else
			
				Draw.Texture( "Graphics/UI/msgbox_rightmouth.png", v.x - campos.x + v.width * .45, v.y - campos.y + v.height - 2, 11, 9 );
			
			end
		
		end
			
		Draw.Rect( v.x - campos.x + 3, v.y - campos.y + 2, v.width - 6, v.height - 5 );
		
		Draw.SetColor( 0, 0, 0, 1 );
		
		local offset = 0;
		local char_to_draw = v.cur_len;
		
		local total_chars = 0;
		
		for n, m in pairs( v.v_text ) do
			
			if( string.len( m ) < char_to_draw ) then
				Draw.Text( "Uni14", m, v.x - campos.x + 7, v.y - campos.y + 4 + offset );
				char_to_draw = char_to_draw - string.len( m );
			else
			
				Draw.Text( "Uni14", string.sub( m, 1, char_to_draw ), v.x - campos.x + 7, v.y - campos.y + 4 + offset );
				break;
				
			end
			
			offset = offset + 14;
		
		end
		
		if( v.cur_len < v.len and GetTicks() > v.next_char_time ) then
			
			v.cur_len = v.cur_len + 1;
			v.next_char_time = GetTicks() + v.speed;
			
		end
		
		if( GetTicks() > v.endtime ) then
			
			MsgBox[k] = nil;
			
		end
				
	end
		
end

UIMessages = { };
function LuaC.TimedUIMessage3( text, x, y, func )
	
	local w = GetStringWidth( "Uni14", text );
	
	LuaC.TimedUIMessage( text, x, y, w, -1, 0, func );
	
end

function LuaC.TimedUIMessage2( text, x, y, time )
	
	local w = GetStringWidth( "Uni14", text );
	
	LuaC.TimedUIMessage( text, x, y, w, time, 0 );
	
end

function LuaC.TimedUIMessage( text, x, y, width, time, delay, func )
	
	table.insert( UIMessages, { 

		text = text,
		x = x, y = y,
		width = width + 20, time = time,
		die_time = time + GetTicks() + delay,
		begin_time = GetTicks() + delay,
		think = func,

	} );


end

function DrawUIMessages()
	

	for k, v in pairs( UIMessages ) do

		local thinkrec = false;
		
		if( v.think ) then
			
			thinkrec = v.think();
			
		end
		
		if( thinkrec or ( v.time > -1 and GetTicks() > v.begin_time ) ) then
			
			Draw.SetColor( 1, 1, 1, 1 );
			               
			Draw.Texture( "Graphics/UI/uigeneric_left.png", v.x, v.y, 7, 20 );
			
			local remainder = 0;
			
			if( v.width > 14 ) then
				
				remainder = v.width - 14;
				Draw.Texture( "Graphics/UI/uigeneric_mid.png", v.x + 7, v.y, remainder, 20 );	

			end
			
			Draw.Texture( "Graphics/UI/uigeneric_right.png", v.x + 7 + remainder, v.y, 7, 20 );
			              
			Draw.Text( "Uni14", v.text, v.x + 10, v.y + 2 );
		
		end
		
		if( ( v.think and not thinkrec ) or ( v.time > -1 and GetTicks() > v.die_time ) ) then
		    UIMessages[k] = nil;
		  end
		

	end
	
	
end
